/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.repository.records.impl;

import cz.mzk.oai.metadataRepository.metadata.MetadataFormat;
import cz.mzk.oai.metadataRepository.repository.Identifier;
import cz.mzk.oai.metadataRepository.repository.collections.MetadataCollection;
import cz.mzk.oai.metadataRepository.repository.records.presentAndDeleted.PresentRecord;
import cz.mzk.rehan.commons.Dom4jUtils;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class FilePresentRecord extends AbstractFileRecord implements PresentRecord {

    private static final Logger logger = Logger.getLogger(FilePresentRecord.class.getName());
    private Document metadata = null;//lazy loaded

    public FilePresentRecord(Identifier identifier, MetadataFormat format, File metadataDir, MetadataCollection collection) {
        super(identifier, format, metadataDir, collection);
    }

    public boolean isDeleted() {
        return false;
    }

    public void setMetadata(Document metadata) {
        this.metadata = metadata;
    }

    public Document getMetadata() throws IOException {
        if (metadata == null) {
            loadMetadata();
        }
        return metadata;
    }

    private void loadMetadata() throws IOException {
        try {
            metadata = Dom4jUtils.loadDocument(getMetadataFile(), false);
        } catch (DocumentException ex) {
            logger.log(Level.SEVERE, "error reading file {0}", getMetadataFile().getAbsolutePath());
            throw new IOException(ex);
        }
    }
}
